package com.kiln.juxingqs.ui.vm


import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import com.kiln.juxingqs.api.bean.UserAddress

class AddrMangerViewModel(app: Application) : MiddleVm(app) {

    val call = MutableLiveData<JsonResult<UserAddress>>()
    val delRet = MutableLiveData<JsonResult<Ret>>()
    val defRet = MutableLiveData<JsonResult<Ret>>()
    val isLoading = MutableLiveData<Boolean>(false)
    val isShowing = MutableLiveData<Boolean>(false)
    val isRef = MutableLiveData<Boolean>(false)

    fun call() {
        hold(
            {
                server.getAddress()
            },
            {
                isRef.value = true
                isLoading.value = true
            }, {
                call.value = it
            }, {
            }, {
                isRef.value = false
                isLoading.value = false
            }
        )
    }

    fun del(add: UserAddress.Address) {
        hold(
            {
                server.del_address(add.address_id)
            },
            {
                isLoading.value = true
            }, {
                delRet.value = it
            }, {
            }, {
                isLoading.value = false
            }
        )
    }

    fun defAddr(add: UserAddress.Address) {
        hold(
            {
                server.address_default(add.address_id)
            },
            {
                isLoading.value = true
            }, {
                defRet.value = it
            }, {
            }, {
                isLoading.value = false
            }
        )
    }
}